package com.tpc.client.modules.core.organisation.datasource;

import com.tpc.client.modules.core.organisation.service.OrganisationService;
import com.tpc.client.modules.core.organisation.service.OrganisationServiceAsync;
import com.tpc.dao.extend.Organisation;

import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSourceField;
import com.tpc.client.systeme.core.datasource.GwtRpcDataSource;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.tpc.client.systeme.core.Systeme;

public class OrganisationListDS extends GwtRpcDataSource {

    Systeme systeme = null;
    String MODULEID = null;
        
    public OrganisationListDS(){
        
    }

    public OrganisationListDS(Systeme sysp,String strID,String modulep) {
            setID(strID);
            systeme = sysp;
            MODULEID = modulep;
            DataSourceField fldOrganisationId = new DataSourceField();
            fldOrganisationId.setName(MODULEID+"_liste_id");
            fldOrganisationId.setType(FieldType.INTEGER);
            fldOrganisationId.setPrimaryKey(Boolean.TRUE);
            fldOrganisationId.setHidden(Boolean.TRUE);
            
            DataSourceField fldOrganisationDescription = new DataSourceField();
            fldOrganisationDescription.setName(MODULEID+"_liste_notes");
            fldOrganisationDescription.setType(FieldType.TEXT);
            fldOrganisationDescription.setPrimaryKey(Boolean.FALSE);
            fldOrganisationDescription.setHidden(Boolean.TRUE);
            
            DataSourceField fldfldOrganisationUrl = new DataSourceField();
            fldfldOrganisationUrl.setName(MODULEID+"_liste_url");
            fldfldOrganisationUrl.setType(FieldType.TEXT);
            fldfldOrganisationUrl.setPrimaryKey(Boolean.FALSE);
            fldfldOrganisationUrl.setHidden(Boolean.TRUE);
            
            DataSourceField fldUrlIcon = new DataSourceField();
            fldUrlIcon.setName(MODULEID+"_liste_urlIcon");
            fldUrlIcon.setType(FieldType.IMAGE);
            fldUrlIcon.setPrimaryKey(Boolean.FALSE);
            fldUrlIcon.setHidden(Boolean.FALSE);
            
            DataSourceField fldOrganisationType = new DataSourceField();
            fldOrganisationType.setName(MODULEID+"_liste_type");
            fldOrganisationType.setType(FieldType.TEXT);
            fldOrganisationType.setPrimaryKey(Boolean.FALSE);
            fldOrganisationType.setHidden(Boolean.FALSE);
            
            DataSourceField fldOrganisationName = new DataSourceField();
            fldOrganisationName.setName(MODULEID+"_liste_name");
            fldOrganisationName.setType(FieldType.TEXT);
            fldOrganisationName.setPrimaryKey(Boolean.FALSE);
            fldOrganisationName.setHidden(Boolean.FALSE);
            
            DataSourceField fldOrganisationPhone = new DataSourceField();
            fldOrganisationPhone.setName(MODULEID+"_liste_phone");
            fldOrganisationPhone.setType(FieldType.TEXT);
            fldOrganisationPhone.setPrimaryKey(Boolean.FALSE);
            fldOrganisationPhone.setHidden(Boolean.FALSE);
            
            DataSourceField fldOrganisationAddress = new DataSourceField();
            fldOrganisationAddress.setName(MODULEID+"_liste_address");
            fldOrganisationAddress.setType(FieldType.TEXT);
            fldOrganisationAddress.setPrimaryKey(Boolean.FALSE);
            fldOrganisationAddress.setHidden(Boolean.FALSE);
            
            DataSourceField fldOrganisationZipcode = new DataSourceField();
            fldOrganisationZipcode.setName(MODULEID+"_liste_zipcode");
            fldOrganisationZipcode.setType(FieldType.TEXT);
            fldOrganisationZipcode.setPrimaryKey(Boolean.FALSE);
            fldOrganisationZipcode.setHidden(Boolean.FALSE);
            
            DataSourceField fldOrganisationCity = new DataSourceField();
            fldOrganisationCity.setName(MODULEID+"_liste_city");
            fldOrganisationCity.setType(FieldType.TEXT);
            fldOrganisationCity.setPrimaryKey(Boolean.FALSE);
            fldOrganisationCity.setHidden(Boolean.FALSE);
            
           setFields(fldOrganisationId,fldOrganisationDescription,fldfldOrganisationUrl,fldUrlIcon,fldOrganisationType,fldOrganisationName,fldOrganisationPhone,fldOrganisationAddress,fldOrganisationZipcode,fldOrganisationCity);
    }

    @Override
    protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
        OrganisationServiceAsync service = GWT.create(OrganisationService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint(systeme.getPageUrl()+"/com.tpc.organisation/orgloader");    
        service.loadGrid(new AsyncCallback<Organisation[]> () {
            public void onFailure (Throwable caught) {
                response.setStatus (RPCResponse.STATUS_FAILURE);
                processResponse (requestId, response);
                systeme.stopLoading();
                systeme.handleServerException(caught);
            }
            public void onSuccess (Organisation[] result) {
                int NbrResultat = 0;
                ListGridRecord[] list = getListGridResultAsRecord(result);
                if (list != null) { NbrResultat = list.length; }
                systeme.getAppBar().getTab(systeme.getDictionary().MODULE_ORGANISATION_ID()).setTitle(systeme.getDictionary().MODULE_ORGANISATION_TITLE()+" ("+NbrResultat+")");
                response.setData(list);
                processResponse(requestId, response);
                systeme.hideSplashScreen();
                systeme.stopLoading();
            }
        });
    }

    @Override
    protected void executeAdd (final String requestId, final DSRequest request, final DSResponse response) {
        //
    }

    @Override
    protected void executeUpdate (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

    @Override
    protected void executeRemove (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }
    
    private ListGridRecord[] getListGridResultAsRecord(Organisation[] result) {
        ListGridRecord[] organisations = null;
        if (result != null) {
            organisations = new ListGridRecord[result.length];
            for (int i = 0; i < organisations.length; i++) {
                organisations[i] = new ListGridRecord();
                String urlIcon = "blank";
                if (result[i].getWebSite() != null) {
                    organisations[i].setAttribute(MODULEID+"_liste_url", result[i].getWebSite().getUrl());
                    if (result[i].getWebSite().getUrl() != null) {
                        if (result[i].getWebSite().getUrl().length() > 4) {
                            urlIcon = "webSiteLink";

                        }
                    }
                }
                organisations[i].setAttribute(MODULEID+"_liste_urlIcon",urlIcon);
                organisations[i].setAttribute(MODULEID+"_liste_id", result[i].getId());
                if (result[i].getType() != null) {
                	organisations[i].setAttribute(MODULEID+"_liste_type", result[i].getType().getName());
                }
                organisations[i].setAttribute(MODULEID+"_liste_name", result[i].getName());
                if (result[i].getPhone() != null) {
                    organisations[i].setAttribute(MODULEID+"_liste_phone", result[i].getPhone().getAreacode()+" "+result[i].getPhone().getNumber());
                }
                if (result[i].getAddress() != null) {
                    organisations[i].setAttribute(MODULEID+"_liste_address", result[i].getAddress().getStreet()+" "+result[i].getAddress().getSuite());
                    organisations[i].setAttribute(MODULEID+"_liste_zipcode", result[i].getAddress().getZipcode());
                    organisations[i].setAttribute(MODULEID+"_liste_city", result[i].getAddress().getCityName());
                }
                organisations[i].setAttribute(MODULEID+"_liste_notes", result[i].getDescription());
            }
        }
        return organisations;
    }
    
    protected void setDataToGrid(Organisation result,ListGrid RELOAD_GRID,int position) {
    	String urlIcon = "blank";
        if (result.getWebSite() != null) {
            RELOAD_GRID.getRecord(position).setAttribute("organisation_liste_url", result.getWebSite().getUrl());
            if (result.getWebSite().getUrl() != null) {
                if (result.getWebSite().getUrl().length() > 4) {
                    urlIcon = "webSiteLink";

                }
            }
        }
        RELOAD_GRID.getRecord(position).setAttribute("organisation_liste_urlIcon",urlIcon);
        RELOAD_GRID.getRecord(position).setAttribute("organisation_liste_id", result.getId());
        RELOAD_GRID.getRecord(position).setAttribute("organisation_liste_name", result.getName());
        if (result.getPhone() != null) {
            RELOAD_GRID.getRecord(position).setAttribute("organisation_liste_phone", result.getPhone().getAreacode()+" "+result.getPhone().getNumber());
        }
        if (result.getAddress() != null) {
            RELOAD_GRID.getRecord(position).setAttribute("organisation_liste_address", result.getAddress().getStreet()+" "+result.getAddress().getSuite());
            RELOAD_GRID.getRecord(position).setAttribute("organisation_liste_zipcode", result.getAddress().getZipcode());
            RELOAD_GRID.getRecord(position).setAttribute("organisation_liste_city", result.getAddress().getCityName());
        }
    }

}